import 'package:ble2025_plugin/ble2203_plugin.dart';
import 'package:ble2025_plugin_example/util/app_all_value.dart';
import 'package:ble2025_plugin_example/util/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

///怀孕周期
class PregnancyCyclePage extends StatefulWidget {
  const PregnancyCyclePage({Key? key}) : super(key: key);

  @override
  State<PregnancyCyclePage> createState() => _PregnancyCyclePageState();
}

class _PregnancyCyclePageState extends State<PregnancyCyclePage> {
  DateTime datetime = DateTime.now();
  AppAllValueController controller = Get.find();

  @override
  void initState() {
    super.initState();
    EventBus().on('dataCallBack', (arg){
      Map map = arg;
      switch (map[DeviceKey.DataType] as String) {
        case BleConst.SetPregnancyCycle:
        case BleConst.GetPregnancyCycle:
          controller.showContentDialog(map.toString());
          break;
      }}
    );

  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //导航栏
        title: Text("怀孕周期".tr),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('读取'.tr),
                  onPressed: (){
                    controller.writeData(BleSDK.readPregnancyCycle());
                  },
                ),
              )),
            ],
          ),
          Container(
            margin: const EdgeInsets.only(top: 10,left: 10,right: 10),
            alignment: Alignment.center,
            child: Text('怀孕周期信息设置'.tr),
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('日期选择'.tr),
                  onPressed: (){
                    showDatePicker(context: context,
                        initialDate: DateTime.now(),
                        firstDate: DateTime(2000,1,1),
                        lastDate: DateTime(2050,12,31)).then((value){
                      if(value != null){
                        setState(() {
                          datetime = value;
                        });
                      }
                    });
                  },
                ),
              )),
              Expanded(child: Container(
                margin: const EdgeInsets.only(right: 10),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('${datetime.year}-${datetime.month}-${datetime.day}'),
                  ],
                ),
              ))
            ],
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('设置'.tr),
                  onPressed: (){
                    controller.writeData(BleSDK.setPregnancyCycle(datetime));
                  },
                ),
              )),

            ],
          )

        ],
      ),
    );
  }
}
